package com.google.code.flabbyraffle;

import com.google.common.collect.Lists;

import javax.swing.event.ListSelectionEvent;
import java.util.ArrayList;
import java.util.List;

import static java.util.Arrays.asList;

public class TicketList extends ArrayList<Ticket> {
    private static final String NEW_LINE = "\\\\n";

    public TicketList(String ticketBatch) {
        for (String ticketText : splitTickets(ticketBatch)) {
            Ticket ticket = new Ticket(ticketText);
            this.add(ticket);
        }
    }

    private static List<String> splitTickets(String ticketBatch) {
        return asList(ticketBatch.split(NEW_LINE));
    }

    public Ticket getByTicketId(Integer ticketId) {
        for (Ticket ticket : this) {
            if (Integer.parseInt(ticket.getId().trim()) == (ticketId)) {
                return ticket;
            }
        }
        throw new RuntimeException("Ticket id not found");
    }
}
